package com.hotel.BL;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.EntityName;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.CompositeFilterOperator;
import com.google.appengine.api.datastore.Query.Filter;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.datastore.Query.FilterPredicate;

import data.Floor;
import data.Reservation;
import data.passingData.FloorData;

public class FloorLogic extends AbstractBL{
	public FloorLogic(HttpServletRequest req,HttpServletResponse resp){
		this.req = req;
		this.resp = resp;
	}
	
	public List<Floor> GenerateFloor(long hotelId)
	{
		List<Floor> list = new ArrayList<Floor>();
		
		Floor floor = new Floor();
		floor.setHotelId(hotelId);
		floor.setTitle("floor1");
		list.add(floor);
		
		floor = new Floor();
		floor.setHotelId(hotelId);
		floor.setTitle("floor2");
		list.add(floor);
		
		floor = new Floor();
		floor.setHotelId(hotelId);
		floor.setTitle("floor3");
		list.add(floor);
		
		return list;
	}
	
	public Floor GetFloor(long l)
	{
		Key key = KeyFactory.createKey(EntityName.Floor, l);
		try {
			Entity lang = datastore.get(key);
			Floor c = new Floor();
			c.setEntity(lang);
			return c;
		} catch (EntityNotFoundException e) {
			return null;
		}
	}
		
	
	public Floor GetFloor(long hotelId,String title)
	{
		Query q = new Query(EntityName.Floor);
		
		Filter filterHotel = new FilterPredicate("hotelId",FilterOperator.EQUAL,hotelId);
		Filter filterStatus = new FilterPredicate("status",FilterOperator.EQUAL,true);
		Filter filterAvaliable = new FilterPredicate("avaliable",FilterOperator.EQUAL,true);
		Filter filterTitle = new FilterPredicate("title",FilterOperator.EQUAL,title);

		Filter filter = CompositeFilterOperator.and(filterHotel, filterStatus, filterAvaliable,filterTitle);
		
		q.setFilter(filter);
		
		return GetFloor(datastore.prepare(q).asSingleEntity());
	}
	
	public List<Floor> GetAllFloorInHotel(long hotelId)
	{
		Query q = new Query(EntityName.Floor);
		
		Filter filterHotel = new FilterPredicate("hotelId",FilterOperator.EQUAL,hotelId);
	
		q.setFilter(filterHotel);
		
		return GetList(datastore.prepare(q).asList(FetchOptions.Builder.withDefaults()));
	}
	
	public List<Floor> GetAllFloor(long hotelId)
	{
		Query q = new Query(EntityName.Floor);
		
		Filter filterHotel = new FilterPredicate("hotelId",FilterOperator.EQUAL,hotelId);
		Filter filterStatus = new FilterPredicate("status",FilterOperator.EQUAL,true);
		Filter filterAvaliable = new FilterPredicate("avaliable",FilterOperator.EQUAL,true);

		Filter filter = CompositeFilterOperator.and(filterHotel, filterStatus, filterAvaliable);
		
		q.setFilter(filter);
		
		return GetList(datastore.prepare(q).asList(FetchOptions.Builder.withDefaults()));
	}
	
	public List<FloorData> GetAllFloor4AdminLimit(long hotelId,long offset,long line)
	{
		Query q = new Query(EntityName.Floor);
		
		Filter filterHotel = new FilterPredicate("hotelId",FilterOperator.EQUAL,hotelId);
		Filter filterAvaliable = new FilterPredicate("avaliable",FilterOperator.EQUAL,true);

		Filter filter = CompositeFilterOperator.and(filterHotel, filterAvaliable);
		
		q.setFilter(filter);
		
		return ReturnData(GetList(datastore.prepare(q).asList(FetchOptions.Builder.withLimit((int) line).offset((int)offset))));
	}

	public List<Floor> GetAllFloor4Admin(long hotelId)
	{
		Query q = new Query(EntityName.Floor);
		
		Filter filterHotel = new FilterPredicate("hotelId",FilterOperator.EQUAL,hotelId);
		Filter filterAvaliable = new FilterPredicate("avaliable",FilterOperator.EQUAL,true);

		Filter filter = CompositeFilterOperator.and(filterHotel, filterAvaliable);
		
		q.setFilter(filter);
		
		return GetList(datastore.prepare(q).asList(FetchOptions.Builder.withDefaults()));
	}
	
	public List<FloorData> ReturnData(List<Floor> list){
		List<FloorData> dataList = new ArrayList<FloorData>();
		for(int i=0;i<list.size();i++){
			FloorData data = new FloorData();
			data.setId(list.get(i).getId());
			data.setHotelId(list.get(i).getHotelId());
			data.setTitle(list.get(i).getTitle());
			data.setStatus(list.get(i).isStatus());
			data.setAvaliable(list.get(i).isAvaliable());
			data.setRoomamount(GetRoomAmount(list.get(i).getHotelId(),list.get(i).getId()));
			dataList.add(data);
		}
		return dataList;
	}
	
	public long GetRoomAmount(long hotelId,long id){
		Query q = new Query(EntityName.Room);

		Filter filterHotel = new FilterPredicate("hotelId",
				FilterOperator.EQUAL, hotelId);
		Filter filterFloor = new FilterPredicate("floor",
				FilterOperator.EQUAL, String.valueOf(id));
		Filter filterStatus = new FilterPredicate("status",
				FilterOperator.EQUAL, true);
		Filter filterAvaliable = new FilterPredicate("avaliable",
				FilterOperator.EQUAL, true);

		Filter filter = CompositeFilterOperator.and(filterHotel,filterFloor, filterStatus,
				filterAvaliable);

		q.setFilter(filter);

		return datastore.prepare(q).asList(FetchOptions.Builder.withDefaults()).size();
	}
	public Floor GetFloor(Entity entity)
	{
		Floor item = new Floor();
		item.setEntity(entity);
		return item;
	}
	
	public List<Floor> GetList(List<Entity> entities)
	{
		List<Floor> list = new ArrayList<Floor>();
		for(Entity e : entities)
		{
			Floor a = new Floor();
			a.setEntity(e);
			list.add(a);
		}
		return list;
	}

	public Floor AddFloor(long hotelId,String title) throws EntityNotFoundException{
		Floor b = new Floor();
		b.setHotelId(hotelId);
		b.setTitle(title);
		b.setAvaliable(true);
		b.setStatus(true);
		
		Key k = datastore.put(b.getEntity());
		b.setId(k.getId());
		b.setKey(k);
		return b;
	}
	
	public Floor EditFloor(long FloorId,String title)throws EntityNotFoundException{
		Floor b = GetFloor(FloorId);
		b.setTitle(title);
		
		Key k = datastore.put(b.getEntity());
		b.setId(k.getId());
		b.setKey(k);

		return b;
	}
	
	public Floor DeleteFloor(long floorId) throws EntityNotFoundException{
		Floor b = GetFloor(floorId);
		b.setAvaliable(false);
		b.setStatus(false);
		
		Key k = datastore.put(b.getEntity());
		b.setId(k.getId());
		b.setKey(k);

		return b;
	}
	
	public Floor ChangeStatus(long floorId) throws EntityNotFoundException{
		
		Floor a = GetFloor(floorId);
		if(a.isStatus() == true){
			a.setStatus(false);
		}else if(a.isStatus() == false){
			a.setStatus(true);
		}
		 
		Key k = datastore.put(a.getEntity());
		a.setId(k.getId());
		a.setKey(k);

		return a;
	}
	
	public boolean CanChangeFloor(long hotelId,long floorId){
		Query q = new Query(EntityName.Reservation);
		Date date = new NightAuditLogic(req,resp).ChangeToNightAuditTime(hotelId,new GenericLogic(req,resp).ServerDateTime());
		Filter filterhotelId = new FilterPredicate("hotelId",
				FilterOperator.EQUAL, hotelId);
		Filter filterDate = new FilterPredicate("dateTo",
				FilterOperator.GREATER_THAN_OR_EQUAL, date);
		Filter filter = CompositeFilterOperator.and(filterhotelId, filterDate);
		q.setFilter(filter);
		List<Reservation> list = new ReservationLogic(req,resp).GetList(datastore.prepare(q).asList(
				FetchOptions.Builder.withDefaults()));
		boolean isFoundUsed = false;
		for(int i=0;i<list.size();i++){
			if(list.get(i).getRoom().getFloor().getId() == floorId){
				isFoundUsed = true;
				break;
			}
		}
		return isFoundUsed;
	}
	
	public Floor UpdateFloor(Floor p)
	{
		Key key = datastore.put(p.getEntity());
		p.setId(key.getId());
		p.setKey(key);
		return p;
	}
}
